package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.OrderService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Reference
    private OrderService orderService;

    //查看可预约
    @RequestMapping("/findOrdersPage")
    public PageResult findOrdersPage(@RequestBody QueryPageBean queryPageBean) throws Exception{

            return orderService.findOrdersPage(queryPageBean);

    }


    @RequestMapping("/submit")
    public Result submit(@RequestBody Map map ,Integer[] setmealIds) throws Exception {

          return orderService.submit(map,setmealIds);

    }

    //给用户发送短信通知
    @RequestMapping("/notification")
    public Result notification(String phoneNumber , String orderDate) throws ClientException {

            //给用户发送短信通知
        if (phoneNumber != null && orderDate != null){
            System.out.println(phoneNumber);
            System.out.println(orderDate);
         //   SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE,phoneNumber,orderDate);
            return new Result(false,MessageConstant.SEND_SHORTNOTE_SUCCESS);
        }

        return new Result(true,MessageConstant.SEND_SHORTNOTE_SUCCESS);
    }

    @RequestMapping("/deleteOrderById")
    public Result deleteOrderById(Integer id) {

        try {
            orderService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ORDER_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_ORDER_SUCCESS);
    }


    @RequestMapping("/edit")
    public Result edit(@RequestBody Map map ,Integer[] setmealIds) throws Exception {
        System.out.println(map);
        return orderService.edit(map,setmealIds);

    }

    @RequestMapping("/switchStatusChange")
    public Result switchStatusChange(@RequestBody Map map) {


        try {
            orderService.switchStatusChange(map);
            return new Result(true, MessageConstant.MODIFIED_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.MODIFIED_FAIL);
        }
    }
}
